#!usr/bin/env python
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tap_connector04_db2.py
@version:
@time: 2022/01/13 
"""
import ibm_db_dbi

from TapClientConnector.connector import connector_query_logger
from TapClientConnector.connector.tap_connector02_oracle import TAPOracleConnector
from TapUtil.logfunc.logger import logger
from db_info import DB2


class TAPDB2Connector(TAPOracleConnector):
    def __init__(self, kwargs):
        super(TAPOracleConnector, self).__init__(kwargs)
        port = self.FORMAT_DB_INFO.pop('port')
        con_str = 'ATTACH=FALSE;'
        con_str += f"PROTOCOL=TCPIP;PORT={port};"
        try:
            self.connector = ibm_db_dbi.connect(dsn=con_str, **self.FORMAT_DB_INFO)
        except Exception as e:
            logger.fatal(e)
            logger.fatal('INIT DB2 CONNECTOR FAILED')

    @connector_query_logger
    def show_dbs(self):
        db_list = []
        for i in self._query("select * from sysibm.sysschemata"):
            db_list.append(i[0])
        return db_list

    @connector_query_logger
    def show_tables(self):
        query_str = f"select name from sysibm.systables where type='T' and creator='{self.connector_schema}'"
        table_list = []
        for i in self._query(query_str):
            table_list.append(i[0])
        return table_list


if __name__ == '__main__':
    T = TAPDB2Connector(DB2.auto_db2_source)
    print(T.show_tables())
    print(T.show_dbs())
